//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by 王道林 on 16/7/26.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *marr;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> * selectIndexArray;
@property (nonatomic, strong) UITableView *tableView;
// 选中名字的数组
@property (nonatomic, strong) NSMutableArray<NSString *> *nameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.selectIndexArray = [NSMutableArray array];
    self.nameArray = [NSMutableArray array];
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪", ];
    self.marr = [NSMutableArray arrayWithArray:arr];
    
    [self createSubview];
    
    
    
}
- (void)createSubview
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"wang"];
    
    
    // tableView  编辑
    
    //设置右BarbuttonItem 为编辑的editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    
    self.editButtonItem.action = @selector(editAction);
    
    
}
- (void)editAction
{
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
}

// 能否移动 , 默认可以移动  可以根据row判断 那行可以移动 那行不行
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 更改数据源..  这个方法就可以实现移动
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [_marr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

// 让tableView进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else
    {
        // 要删除选中得数组
        //  删除数据源
        [_marr removeObjectsInArray:_nameArray];
        
        //  删除 cell
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationLeft];
        
        [_selectIndexArray removeAllObjects];
        [_nameArray removeAllObjects];
        NSLog(@"完成");
    }
}

//  控制tableView能否进行编辑, NO不能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark -  delegate
//返回cell的编辑状态(样式)默认为delete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //   多选    或运算
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}

//  提交编辑  ,  就是对应按钮的点击方法
//  滑动删除, 只和这个方法有关系
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        // 如果对tableView进行添加/删除.  先修改数据源. 再去对应的添加/ 删除
        [_marr removeObjectAtIndex:indexPath.row];
        // 2 . 对应修改cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    } else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        [_marr insertObject:@"王黄" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        
        NSLog(@"添加");
    }
    //  不论是删除和添加, 都已经进行了刷新
}



#pragma mark - tableview delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _marr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wang" forIndexPath:indexPath];
    cell.textLabel.text = _marr[indexPath.row];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.editing) {
        // 在全选状态下, 数组中添加对应选中的下标
        [_nameArray addObject:_marr[indexPath.row]];
        [_selectIndexArray addObject:indexPath];
        NSLog(@"%ld", _selectIndexArray.count);
    }
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.editing) {
        [_nameArray removeObject:_marr[indexPath.row]];
        [_selectIndexArray removeObject:indexPath];
    }
    
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"删除");
    }];
                                     
    return @[action1];
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
